<?php
/**
 * Class that operate on table 'event_movies'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class EventMoviesMySqlDAO implements EventMoviesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return EventMoviesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM event_movies WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM event_movies';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM event_movies ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param eventMovie primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM event_movies WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param EventMoviesMySql eventMovie
 	 */
	public function insert($eventMovie){
		$sql = 'INSERT INTO event_movies (event_id, movie_name, star_cast, director, producer, released_date, discription, banner_image, logo, star_casts_id, youtube_url, created_by, created_date, updated_by, updated_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($eventMovie->eventId);
		$sqlQuery->set($eventMovie->movieName);
		$sqlQuery->set($eventMovie->starCast);
		$sqlQuery->set($eventMovie->director);
		$sqlQuery->set($eventMovie->producer);
		$sqlQuery->set($eventMovie->releasedDate);
		$sqlQuery->set($eventMovie->discription);
		$sqlQuery->set($eventMovie->bannerImage);
		$sqlQuery->set($eventMovie->logo);
		$sqlQuery->setNumber($eventMovie->starCastsId);
		$sqlQuery->set($eventMovie->youtubeUrl);
		$sqlQuery->setNumber($eventMovie->createdBy);
		$sqlQuery->set($eventMovie->created_date);
		$sqlQuery->setNumber($eventMovie->updatedBy);
		$sqlQuery->set($eventMovie->updated_date);
		$sqlQuery->setNumber($eventMovie->status);

		$id = $this->executeInsert($sqlQuery);	
		$eventMovie->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param EventMoviesMySql eventMovie
 	 */
	public function update($eventMovie){
		$sql = 'UPDATE event_movies SET event_id = ?, movie_name = ?, star_cast = ?, director = ?, producer = ?, released_date = ?, discription = ?, banner_image = ?, logo = ?, star_casts_id = ?, youtube_url = ?, created_by = ?, created_date = ?, updated_by = ?, updated_date = ?, status = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($eventMovie->eventId);
		$sqlQuery->set($eventMovie->movieName);
		$sqlQuery->set($eventMovie->starCast);
		$sqlQuery->set($eventMovie->director);
		$sqlQuery->set($eventMovie->producer);
		$sqlQuery->set($eventMovie->releasedDate);
		$sqlQuery->set($eventMovie->discription);
		$sqlQuery->set($eventMovie->bannerImage);
		$sqlQuery->set($eventMovie->logo);
		$sqlQuery->setNumber($eventMovie->starCastsId);
		$sqlQuery->set($eventMovie->youtubeUrl);
		$sqlQuery->setNumber($eventMovie->createdBy);
		$sqlQuery->set($eventMovie->created_date);
		$sqlQuery->setNumber($eventMovie->updatedBy);
		$sqlQuery->set($eventMovie->updated_date);
		$sqlQuery->setNumber($eventMovie->status);

		$sqlQuery->setNumber($eventMovie->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM event_movies';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByEventId($value){
		$sql = 'SELECT * FROM event_movies WHERE event_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMovieName($value){
		$sql = 'SELECT * FROM event_movies WHERE movie_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStarCast($value){
		$sql = 'SELECT * FROM event_movies WHERE star_cast = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDirector($value){
		$sql = 'SELECT * FROM event_movies WHERE director = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByProducer($value){
		$sql = 'SELECT * FROM event_movies WHERE producer = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReleasedDate($value){
		$sql = 'SELECT * FROM event_movies WHERE released_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDiscription($value){
		$sql = 'SELECT * FROM event_movies WHERE discription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBannerImage($value){
		$sql = 'SELECT * FROM event_movies WHERE banner_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLogo($value){
		$sql = 'SELECT * FROM event_movies WHERE logo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStarCastsId($value){
		$sql = 'SELECT * FROM event_movies WHERE star_casts_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByYoutubeUrl($value){
		$sql = 'SELECT * FROM event_movies WHERE youtube_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedBy($value){
		$sql = 'SELECT * FROM event_movies WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM event_movies WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedBy($value){
		$sql = 'SELECT * FROM event_movies WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM event_movies WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM event_movies WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByEventId($value){
		$sql = 'DELETE FROM event_movies WHERE event_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMovieName($value){
		$sql = 'DELETE FROM event_movies WHERE movie_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStarCast($value){
		$sql = 'DELETE FROM event_movies WHERE star_cast = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDirector($value){
		$sql = 'DELETE FROM event_movies WHERE director = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByProducer($value){
		$sql = 'DELETE FROM event_movies WHERE producer = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReleasedDate($value){
		$sql = 'DELETE FROM event_movies WHERE released_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDiscription($value){
		$sql = 'DELETE FROM event_movies WHERE discription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBannerImage($value){
		$sql = 'DELETE FROM event_movies WHERE banner_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLogo($value){
		$sql = 'DELETE FROM event_movies WHERE logo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStarCastsId($value){
		$sql = 'DELETE FROM event_movies WHERE star_casts_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByYoutubeUrl($value){
		$sql = 'DELETE FROM event_movies WHERE youtube_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedBy($value){
		$sql = 'DELETE FROM event_movies WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM event_movies WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedBy($value){
		$sql = 'DELETE FROM event_movies WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM event_movies WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM event_movies WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return EventMoviesMySql 
	 */
	protected function readRow($row){
		$eventMovie = new EventMovie();
		
		$eventMovie->id = $row['id'];
		$eventMovie->eventId = $row['event_id'];
		$eventMovie->movieName = $row['movie_name'];
		$eventMovie->starCast = $row['star_cast'];
		$eventMovie->director = $row['director'];
		$eventMovie->producer = $row['producer'];
		$eventMovie->releasedDate = $row['released_date'];
		$eventMovie->discription = $row['discription'];
		$eventMovie->bannerImage = $row['banner_image'];
		$eventMovie->logo = $row['logo'];
		$eventMovie->starCastsId = $row['star_casts_id'];
		$eventMovie->youtubeUrl = $row['youtube_url'];
		$eventMovie->createdBy = $row['created_by'];
		$eventMovie->created_date = $row['created_date'];
		$eventMovie->updatedBy = $row['updated_by'];
		$eventMovie->updated_date = $row['updated_date'];
		$eventMovie->status = $row['status'];

		return $eventMovie;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return EventMoviesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>